<?php
namespace App\Domain;
use App\Model\Music as Model_Music;

class Music {

    /* 分类 */
	public function getCLass() {

        $key='music_class';
		$list=\App\getcaches($key);
		if(!$list){
            $model = new Model_Music();
			$list= $model->getCLass();
			\App\setcaches($key,$list);
		}
        
        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $list[$k]=$v;
        }

		return $list;
	}

    /* 分类下音乐 */
	public function getCLassMusic($uid,$classid,$p) {
        
        $where=[];
        
        $where['classid=?']=$classid;

		$model = new Model_Music();
        
        $list = $model->getMusicList($where,$p);

        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['href']=\App\get_upload_path($v['href']);
            
            $v['iscollection']=$model->isCollection($uid,$v['id']);
            $list[$k]=$v;
        }

		return $list;
	}

    /* 热门 */
	public function getHot($uid,$p) {
        
        $where=[];

		$model = new Model_Music();
        $list = $model->getMusicList($where,$p);

        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['href']=\App\get_upload_path($v['href']);
            
            $v['iscollection']=$model->isCollection($uid,$v['id']);
            $list[$k]=$v;
        }

		return $list;
	}

    /* 搜索 */
	public function search($uid,$key,$p) {
        
        $where=[];
        
        $where['title like ?']='%'.$key.'%';

		$model = new Model_Music();
        $list = $model->getMusicList($where,$p);

        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['href']=\App\get_upload_path($v['href']);
            
            $v['iscollection']=$model->isCollection($uid,$v['id']);
            $list[$k]=$v;
        }

		return $list;
	}

    /* 我的收藏 */
	public function getCollection($uid,$p) {
        
        $where='';
        $list=[];

		$model = new Model_Music();
        $list_music = $model->getCollection($uid);
        
        if(!$list_music){
            return $list;
        }
        $ids=array_column($list_music,'musicid');
        
        $where=' id in ('.implode(',',$ids).')';
        
        $list = $model->getMusicList($where,$p);

        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['href']=\App\get_upload_path($v['href']);
            
            $v['iscollection']=$model->isCollection($uid,$v['id']);
            $list[$k]=$v;
        }

		return $list;
	}

    /* 收藏/取消 */
	public function setCollection($uid,$musicid) {
        
        $where=[];
        $list=[];

		$model = new Model_Music();
        $isexist = $model->isCollection($uid,$musicid);
        $iscollection='1';
        if($isexist){
            /* 已收藏 取消 */
            $model->cancelCollection($uid,$musicid);
            $iscollection='0';
        }else{
            /* 未收藏 收藏 */
            $model->setCollection($uid,$musicid);
            $iscollection='1';
        }


		return $iscollection;
	}
    
    /* 更新使用量 */
	public function upUses($musicid,$num) {
        
        $num=(int)$num;
        
        if( $num==0 ){
            return 1;
        }

		$model = new Model_Music();
        $isexist = $model->upUses($musicid,$num);

		return $iscollection;
	}    
	
}
